//
//  AllOrderViewController.m
//  Zhiche-Carriage
//
//  Created by LeeBruce on 16/8/1.
//  Copyright © 2016年 LeeBruce. All rights reserved.
//

#import "AllOrderViewController.h"
#import "Common.h"
#import "WKProgressHUD.h"
//#import  <UIImageView+WebCache.h>
//#import <SDWebImage/UIButton+WebCache.h>
#import  <UIButton+WebCache.h>
#import "ScrollImageViewController.h"



@interface AllOrderViewController ()

@property (nonatomic,strong) UIButton *button1;
@property (nonatomic,strong) UIButton *button2;
@property (nonatomic,strong) UIButton *button3;
@property (nonatomic,strong) UIButton *button4;

@end

@implementation AllOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];

//    [self initSubView];
    
    [self initDataSource];
    
}

-(void)initDataSource
{
    NSString *string = [NSString stringWithFormat:@"%@/%@?id=%@",mission_detail,self.orderId,self.orderId];
    
    [Common requestWithUrlString:string contentType:application_json finished:^(id responseObj) {
        
        if ([responseObj[@"success"] boolValue]) {
            
            self.dataDictionary = [NSMutableDictionary dictionaryWithDictionary:responseObj[@"data"]];
            
            if ([[self.dataDictionary allKeys]count] > 0) {
                
                if (self.hiddenPriceTeger != 10) {
                    //公司派单
                    self.tableView.frame = CGRectMake(0, cellHeight, screenWidth, cellHeight * (10 + [self.dataDictionary[@"vehicles"] count] - cellHeight * 2 - 10) );
                    //个人派单
                } else {
                    self.tableView.frame = CGRectMake(0, cellHeight, screenWidth, cellHeight * (10 + [self.dataDictionary[@"vehicles"] count]) );
                    
                }
                
//                self.tableView.frame = CGRectMake(0, cellHeight, screenWidth, cellHeight * (10 + [self.dataDictionary[@"vehicles"] count]) );
                
                [self initSubView];
                
                self.orderNumberL.text = self.dataDictionary[@"waybillCode"];
                
                [self.tableView reloadData];
                
                [self getValueString];
            }
            
        } else {
            
            //获取失败信息
            [WKProgressHUD popMessage:responseObj[@"message"] inView:self.view duration:1.5 animated:YES];
            
        }
        
        
    } failed:^(NSString *errorMsg) {
        NSLog(@"%@",errorMsg);
        
    }];
    
    
}

-(void)getValueString
{
    
    
    for (int i = 0; i < [self.dataDictionary[@"sendPics"] count]; i ++) {
        
        if ([self.dataDictionary[@"sendPics"][i][@"type"] integerValue] == 2010) {
            
            NSString *string1 = [NSString stringWithFormat:@"%@",self.dataDictionary[@"sendPics"][i][@"picKey"]];
            NSURL *url = [NSURL URLWithString:string1];
            
            [self.button1 sd_setImageWithURL:url forState:UIControlStateNormal];
            
            
            
        } else {
            
            NSString *string1 = [NSString stringWithFormat:@"%@",self.dataDictionary[@"sendPics"][i][@"picKey"]];
            NSURL *url = [NSURL URLWithString:string1];
            [self.button2 sd_setImageWithURL:url forState:UIControlStateNormal];
        }
    }
    
    
    for (int i = 0; i < [self.dataDictionary[@"deliverPics"] count]; i ++) {
        
        if ([self.dataDictionary[@"deliverPics"][i][@"type"] integerValue] == 3010) {
            
            NSString *string1 = [NSString stringWithFormat:@"%@",self.dataDictionary[@"deliverPics"][i][@"picKey"]];
            NSURL *url = [NSURL URLWithString:string1];
            [self.button3 sd_setImageWithURL:url forState:UIControlStateNormal];
            
            
        } else {
            
            NSString *string1 = [NSString stringWithFormat:@"%@",self.dataDictionary[@"deliverPics"][i][@"picKey"]];
            NSURL *url = [NSURL URLWithString:string1];
            [self.button4 sd_setImageWithURL:url forState:UIControlStateNormal];
        }
    }

    
    
}

-(void)initSubView
{
 
    if ([self.dataDictionary[@"sendPics"] count] > 0) {
        
        UIView *firstV = [self backViewWithFrame:CGRectMake(0,  CGRectGetMaxY(self.tableView.frame) - 10 , screenWidth, cellHeight + 100 * kHeight) andString:@"发车照片"];
        
        //交接单照片
        self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button1.tag = 500;
//        self.button1.userInteractionEnabled = NO;
        UIView *jiaojieV = [self backViewWithButton:self.button1 andString:@"交接单照片" andFloat:18];
        
        [firstV addSubview:jiaojieV];
        
        //车辆照片
        self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button2.tag = 501;
//        self.button2.userInteractionEnabled = NO;
        
        UIView *carV = [self backViewWithButton:self.button2 andString:@"车辆照片" andFloat:38 + 70 * kHeight ];
        
        [firstV addSubview:carV];
        
        self.scrollView.contentSize = CGSizeMake(screenWidth, CGRectGetMaxY(firstV.frame) + 20);


    }
    
    
    if ([self.dataDictionary[@"deliverPics"] count] > 0) {
        
        UIView *secondV = [self backViewWithFrame:CGRectMake(0, CGRectGetMaxY(self.tableView.frame) + 10 + cellHeight + 100 * kHeight , screenWidth, cellHeight + 100 * kHeight) andString:@"卸车照片"];
        
        //交接单照片
        self.button3 = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button3.tag = 502;
        UIView *jiaojie1V = [self backViewWithButton:self.button3 andString:@"交接单照片" andFloat:18];
        
        [secondV addSubview:jiaojie1V];
        
        //车辆照片
        self.button4 = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button4.tag = 503;
        UIView *car1V = [self backViewWithButton:self.button4 andString:@"车辆照片" andFloat:38 + 70 * kWidth];
        
        [secondV addSubview:car1V];
        
        self.scrollView.contentSize = CGSizeMake(screenWidth, CGRectGetMaxY(secondV.frame) + 20);

    }
    
  
}

-(UIView *)backViewWithFrame:(CGRect)frame andString:(NSString *)string
{
    UIView *view = [[UIView alloc]initWithFrame:frame];
    view.backgroundColor = WhiteColor;
    [self.scrollView addSubview:view];
    
    UILabel *titleL = [[UILabel alloc]initWithFrame:CGRectMake(18, 0, 100, cellHeight)];
    titleL.text = string;
    titleL.textColor = littleBlackColor;
    titleL.font = Font(14);
    [view addSubview:titleL];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, cellHeight, screenWidth, 0.5)];
    label.backgroundColor = LineGrayColor;
    [view addSubview:label];
    
    return view;
    
}


-(UIView *)backViewWithButton:(UIButton *)button andString:(NSString *)string andFloat:(CGFloat)x
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(x, cellHeight + 10 * kHeight, 70 * kWidth, 90 * kHeight)];
    
    button.frame = CGRectMake(0, 0, 70 * kHeight, 70 * kHeight);
    [button setBackgroundImage:[UIImage imageNamed:@"car_add"] forState:UIControlStateNormal];
    [view addSubview:button];
    [button addTarget:self action:@selector(scrollBigImg:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMidX(button.frame) - 45, CGRectGetMaxY(button.frame), 90, 20 * kHeight)];
    label.text = string;
    label.font = Font(10);
    label.textColor = carScrollColor;
    label.textAlignment = NSTextAlignmentCenter;
    
    [view addSubview:label];
    return view;
}

-(void)scrollBigImg:(UIButton *)sender
{
    if (sender.tag == 500 || sender.tag == 501) {
        
        NSLog(@"%@",self.dataDictionary[@"sendPics"]);
                     
        if ([self.dataDictionary[@"sendPics"] count] > 0) {
            
            NSMutableArray *arr = [NSMutableArray array];
            for (int i = 0; i < [self.dataDictionary[@"sendPics"] count]; i ++) {
                
                NSString *string1 = [NSString stringWithFormat:@"%@",self.dataDictionary[@"sendPics"][i][@"picKey"]];
                NSURL *url = [NSURL URLWithString:string1];
                [arr addObject:url];
                
            }
            
            
            ScrollImageViewController *scrollV = [[ScrollImageViewController alloc]init];
            scrollV.imageArr = arr;
            
            [self.navigationController pushViewController:scrollV animated:YES];

        }

    }
    
    
    if (sender.tag == 502 || sender.tag == 503) {
        
        if ([self.dataDictionary[@"deliverPics"] count] > 0) {
            
            NSMutableArray *arr = [NSMutableArray array];
            for (int i = 0; i < [self.dataDictionary[@"deliverPics"] count]; i ++) {
                
                NSString *string1 = [NSString stringWithFormat:@"%@",self.dataDictionary[@"deliverPics"][i][@"picKey"]];
                NSURL *url = [NSURL URLWithString:string1];
                [arr addObject:url];
                
            }
            
            ScrollImageViewController *scrollV = [[ScrollImageViewController alloc]init];
            scrollV.imageArr = arr;
            
            [self.navigationController pushViewController:scrollV animated:YES];
            
        }
        
    }

    
    
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
